﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc
{
    public partial class frmTK_DonThuoc : ModuleDuoc.Base.frmTK_Base
    {
        public frmTK_DonThuoc()
        {
            InitializeComponent();
        }

        private void frmTK_DonThuoc_Load(object sender, EventArgs e)
        {
            this.dgvDonThuoc.AutoGenerateColumns = false;
            this.dgvDonThuocCT.AutoGenerateColumns = false;
        }

        private void getAllDonThuoc()
        {
            string sql;
            //string sql = "SELECT * FROM zD_DonThuoc";
            if (this.cboxBS.Enabled == true)
            {
                sql = "SELECT zd.DonThuocId, zd.manhanvien, zd.ngaylap,zd.ketLuanBenh, zd.loiDan,zd.tenKH, zd.diachiKH, zd.dienThoaiKH FROM zD_DONTHUOC zd WHERE zd.ngaylap BETWEEN '" + this.dateFrom.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' AND '" + this.dateTo.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' AND zd.manhanvien LIKE N'%"+ this.cboxBS.Text +"%' ORDER BY zd.DonThuocId desc";
            }
            else
            {
                sql = "SELECT zd.DonThuocId, zd.manhanvien, zd.ngaylap,zd.ketLuanBenh, zd.loiDan,zd.tenKH, zd.diachiKH, zd.dienThoaiKH FROM zD_DONTHUOC zd WHERE zd.ngaylap BETWEEN '" + this.dateFrom.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' AND '" + this.dateTo.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' ORDER BY zd.DonThuocId desc"; 
            }
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            //if (dt.Rows.Count > 0)
            {
                this.dgvDonThuoc.DataSource = dt;
                this.btnTongSoDon.Text = "Tổng số đơn: " + dt.Rows.Count.ToString();
            }
        }
        private void getDonThuocCT()
        {
            string sql = "SELECT * FROM zD_DonThuocCT where donthuocId = " + this.dgvDonThuoc.Rows[this.dgvDonThuoc.CurrentRow.Index].Cells["donthuocID"].Value.ToString();
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.dgvDonThuocCT.DataSource = dt;

            }
        }
        public override void btnView_Click(object sender, EventArgs e)
        {
            base.btnView_Click(sender, e);
            this.getAllDonThuoc();
        }

        private void dgvDonThuoc_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.getDonThuocCT();
        }

        private void chkBS_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkBS.Checked == true)
            {
                this.cboxBS.Enabled = false;
            }
            else
            {
                this.cboxBS.Enabled = true;
                fillTenBS();
            }
        }

        private void fillTenBS()
        {
            string sql = "SELECT zd.manhanvien FROM zD_DONTHUOC zd GROUP BY zd.manhanvien";
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.cboxBS.DataSource = dt;
            cboxBS.DisplayMember = "MaNhanVien"; //ten
            cboxBS.ValueMember = "MaNhanVien"; //ma

            //this.cboxSP.DataSource = bc.GetAll();
            //this.cboxSP.DisplayMember = "TenVT";
            //this.cboxSP.ValueMember = "MaVT";
        }
    }
}
